<template>
  <div id="list">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item><a href="/home">首页</a></el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="searchValue" clearable @clear="getGoodsList()">
            <template #append>
              <i class="el-icon-search" @click="searchByInput()" style="cursor: pointer;"></i>
            </template>
          </el-input>
        </el-col>
        <el-col :span="5">
          <el-button type="primary" class="add-user-btn">添加用户</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="120px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="145px"></el-table-column>
        <el-table-column label="商品名称" prop="add_time" width="170px"></el-table-column>
        <el-table-column label="操作" width="120px">
          <template v-slot:default="scope">
            {{scope.id}}
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>

    <!-- 编辑对话框区域 -->
    <el-dialog title="编辑商品" v-model="editFormVisible">
    <el-form :model="editUserObj" rules="editUserRules" ref="editUserRef">
      <el-form-item label="用户名" label-width="120px">
        <el-input v-model="editUserObj.username" disabled></el-input>
      </el-form-item>
      <el-form-item label="邮箱" label-width="120px" prop="email">
        <el-input v-model="editUserObj.email"></el-input>
      </el-form-item>
      <el-form-item label="手机号" label-width="120px" prop="mobile">
        <el-input v-model="editUserObj.mobile"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sendEditInfo()">确 定</el-button>
      </span>
    </template>
  </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'list',
  data() {
    return {
      // 查询关键字
      searchValue: '',
      // 商品列表
      goodsList: [],
      // 商品总数
      total: 0,
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      editGoodObj: {
        goods_name: ' ',
        goods_price: 0,
        goods_number: 0,
        goods_weight: 0,
        goods_introduce: '',
        pics: [],
        attrs: []
      },
      // 编辑对话框是否展示
      editFormVisible: false,

    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 初始化商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品数据失败！')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
      return this.$message.success('获取商品数据成功！')
    },
    // 监听一页最多展示多少条数据变化
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听页码变化
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 根据关键字拆线呢
    searchByInput() {
      this.queryInfo.query = this.searchValue
      if(this.searchValue.trim().length > 0) {
        this.getGoodsList()
      }
      this.queryInfo.query = ''
    },

  }
}
</script>

<style lang="less" scoped>
.add-user-btn { margin-left: 20px; }

.el-table { margin: 15px 0; }

</style>
